/*
 * pps_bom
 */
package com.pitop.project.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baidu.fsg.uid.UidGenerator;
import com.google.common.collect.Maps;
import com.pitop.core.base.BaseDAO;
import com.pitop.core.base.BaseSVImpl;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.ProjectBomException;
import com.pitop.core.tools.StringTools;
import com.pitop.project.dao.ProjectBomDAO;
import com.pitop.project.entity.ProjectBom;
import com.pitop.project.entity.ProjectBomState;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service("projectBomSV")
@Slf4j
public class ProjectBomSVImpl extends BaseSVImpl<ProjectBom, Long> implements ProjectBomSV {

    @Autowired
    private ProjectBomDAO projectBomDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return projectBomDAO;
    }

    /**
     * 保存account对象
     *
     * @param entity 实体
     * @throws BaseException
     */
    @Override
    public void save(ProjectBom entity) throws BaseException {
        if (entity == null) {
            throw new ProjectBomException(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringTools.isEmpty(entity.getProjectCode())) {
            throw new ProjectBomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (StringTools.isEmpty(entity.getBomCode())) {
            throw new ProjectBomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        //批量添加projectBom
        List<ProjectBom> projectBomList = new ArrayList<>();
        ProjectBom projectBom;
        String[] bomCodeAry = entity.getBomCode().split(",");
        Map<String, Object> map = Maps.newHashMap();

        for (String bomCode : bomCodeAry) {
            projectBom = new ProjectBom();
            projectBom.setProjectCode(entity.getProjectCode());
            projectBom.setBomCode(bomCode);

            //判断是否重复,若未重复则添加
            map.clear();
            map.put("projectCode", entity.getProjectCode());
            map.put("bomCode", bomCode);
            if (projectBomDAO.load(map) == null) {
                projectBomList.add(projectBom);
            }
        }
        projectBomDAO.batchInsert(projectBomList);
    }

    /**
     * 加载一个对象ProjectBom
     *
     * @param id
     * @param projectCode 项目编码
     * @param bomCode     bom编码
     * @return ProjectBom
     */
    @Override
    public ProjectBom load(Long id, String projectCode, String bomCode) {
        if (id == null && projectCode == null && bomCode == null) {
            throw new ProjectBomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("projectCode", projectCode);
        param.put("bomCode", bomCode);
        return projectBomDAO.load(param);
    }

    /**
     * 加载一个对象ProjectBom,(将查询关联数据)
     *
     * @param id
     * @param projectCode 项目编码
     * @param bomCode     bom编码
     * @return ProjectBom
     */
    @Override
    public ProjectBom get(Long id, String projectCode, String bomCode) {
        if (id == null && projectCode == null && bomCode == null) {
            throw new ProjectBomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("projectCode", projectCode);
        param.put("bomCode", bomCode);
        return projectBomDAO.getDetail(id, projectCode, bomCode);
    }

    /**
     * 加载一个对象ProjectBom 通过id
     *
     * @param id
     * @return ProjectBom
     */
    @Override
    public ProjectBom loadById(Long id) {
        if (id == null) {
            throw new ProjectBomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return projectBomDAO.loadById(id);
    }

    /**
     * 加载一个对象ProjectBom 通过projectCode
     *
     * @param projectCode 项目编码
     * @return ProjectBom
     */
    @Override
    public ProjectBom loadByProjectCode(String projectCode) {
        if (projectCode == null) {
            throw new ProjectBomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return projectBomDAO.loadByProjectCode(projectCode);
    }

    /**
     * 加载一个对象ProjectBom 通过bomCode
     *
     * @param bomCode bom编码
     * @return ProjectBom
     */
    @Override
    public ProjectBom loadByBomCode(String bomCode) {
        if (bomCode == null) {
            throw new ProjectBomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return projectBomDAO.loadByBomCode(bomCode);
    }

    /**
     * 加载一个对象ProjectBom 通过id,(将查询关联数据)
     *
     * @param id
     * @return ProjectBom
     */
    @Override
    public ProjectBom getById(Long id) {
        if (id == null) {
            throw new ProjectBomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return projectBomDAO.getById(id);
    }

    /**
     * 加载一个对象ProjectBom 通过projectCode,(将查询关联数据)
     *
     * @param projectCode 项目编码
     * @return ProjectBom
     */
    @Override
    public ProjectBom getByProjectCode(String projectCode) {
        if (projectCode == null) {
            throw new ProjectBomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return projectBomDAO.getByProjectCode(projectCode);
    }

    /**
     * 加载一个对象ProjectBom 通过bomCode,(将查询关联数据)
     *
     * @param bomCode bom编码
     * @return ProjectBom
     */
    @Override
    public ProjectBom getByBomCode(String bomCode) {
        if (bomCode == null) {
            throw new ProjectBomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return projectBomDAO.getByBomCode(bomCode);
    }

    /**
     * 根据主键id,oldStates 共同更新 ProjectBom 的状态到newState状态
     *
     * @param id
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateById(Long id, ProjectBomState newState, ProjectBomState... oldStates) {
        if (id == null) {
            throw new ProjectBomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new ProjectBomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new ProjectBomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        projectBomDAO.updateStateById(id, new Date(), newState.name(), states);
    }

    /**
     * 根据主键projectCode,oldStates 共同更新 ProjectBom 的状态到newState状态
     *
     * @param projectCode 项目编码
     * @param newState    新状态
     * @param oldStates   旧状态集合
     */
    @Override
    public void updateStateByProjectCode(String projectCode, ProjectBomState newState, ProjectBomState... oldStates) {
        if (projectCode == null) {
            throw new ProjectBomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new ProjectBomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new ProjectBomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        projectBomDAO.updateStateByProjectCode(projectCode, new Date(), newState.name(), states);
    }

    /**
     * 根据主键bomCode,oldStates 共同更新 ProjectBom 的状态到newState状态
     *
     * @param bomCode   bom编码
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateByBomCode(String bomCode, ProjectBomState newState, ProjectBomState... oldStates) {
        if (bomCode == null) {
            throw new ProjectBomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new ProjectBomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new ProjectBomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        projectBomDAO.updateStateByBomCode(bomCode, new Date(), newState.name(), states);
    }

    /**
     * 根据主键id 更新 ProjectBom 的状态到另一个状态
     *
     * @param id
     * @param state 状态
     */
    @Override
    public void updateById(Long id, ProjectBomState state) {
        if (id == null) {
            throw new ProjectBomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        projectBomDAO.updateById(id, state.name(), new Date());
    }

    /**
     * 根据主键projectCode 更新 ProjectBom 的状态到另一个状态
     *
     * @param projectCode 项目编码
     * @param state       状态
     */
    @Override
    public void updateByProjectCode(String projectCode, ProjectBomState state) {
        if (projectCode == null) {
            throw new ProjectBomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        projectBomDAO.updateByProjectCode(projectCode, state.name(), new Date());
    }

    /**
     * 根据主键bomCode 更新 ProjectBom 的状态到另一个状态
     *
     * @param bomCode bom编码
     * @param state   状态
     */
    @Override
    public void updateByBomCode(String bomCode, ProjectBomState state) {
        if (bomCode == null) {
            throw new ProjectBomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        projectBomDAO.updateByBomCode(bomCode, state.name(), new Date());
    }


    /**
     * 删除对象ProjectBom
     *
     * @param id * @param projectCode 项目编码* @param bomCode bom编码
     * @return ProjectBom
     */
    @Override
    public void delete(Long id, String projectCode, String bomCode) {
        if (id == null && projectCode == null && bomCode == null) {
            throw new ProjectBomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("projectCode", projectCode);
        param.put("bomCode", bomCode);
        projectBomDAO.delete(param);
    }


    /**
     * 查询ProjectBom分页
     *
     * @param projectBom 对象
     * @param offset     查询开始行
     * @param limit      查询行数
     * @return List<ProjectBom>
     */
    @Override
    public List<ProjectBom> list(ProjectBom projectBom, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }

        Map<String, Object> map = null;
        if (projectBom != null) {
            map = JSON.parseObject(JSON.toJSONString(projectBom, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return projectBomDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public int count(ProjectBom projectBom) {
        Map<String, Object> map = null;
        if (projectBom != null) {
            map = JSON.parseObject(JSON.toJSONString(projectBom, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return projectBomDAO.count(map);
    }

    /**
     * 查询ProjectBom分页
     *
     * @param id     * @param projectCode  项目编码* @param bomCode  bom编码
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<ProjectBom>
     */
    @Override
    public List<ProjectBom> list(Long id, String projectCode, String bomCode, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("projectCode", projectCode);
        param.put("bomCode", bomCode);
        return projectBomDAO.list(param, new RowBounds(offset, limit));
    }

    @Override
    public int count(Long id, String projectCode, String bomCode) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("projectCode", projectCode);
        param.put("bomCode", bomCode);
        return projectBomDAO.count(param);
    }

}
